/* wifi 直连控制*/
#include <Arduino.h>
#include <WiFi.h>
#include <ESP32Servo.h> //舵机

#define LED_BUILTIN 2   // 开发板的指示灯

#define KEY_SERVO_PIN  13 //手指舵机PWM控制脚
#define OFF_POS 0 //手指抬起位置
#define ON_POS 60 //手指按下位置

#define UDP_SERVER_PORT 19988 //UDP服务端口号

Servo exeKeyServo; //手指舵机 

IPAddress localIp=IPAddress(192,168,99,1);
IPAddress gateway=IPAddress(192,168,99,1);
IPAddress subnet=IPAddress(255,255,255,0);

//const char* ssid     = "MAKEBATOR";
//const char* password = "makebator827";

const char* localSsid     = "Finger";
const char* localPass = "!34rste@_8765";

//WiFiServer server(9981);//接收数据服务
WiFiUDP udp;

const uint8_t bfLength=50;//缓冲区长度
uint8_t buff[bfLength];//收发缓冲区

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);//

  delay(500);
  WiFi.disconnect(); 

  Serial.println();
  Serial.println("Configuring access point...");

  //舵机初始化
  // Allow allocation of all timers
  //ESP32PWM::allocateTimer(0);
  //ESP32PWM::allocateTimer(1);
  //ESP32PWM::allocateTimer(2);
  //ESP32PWM::allocateTimer(3);
  //kaServo.setPeriodHertz(50);    // standard 50 hz servo
  exeKeyServo.attach(KEY_SERVO_PIN); // , 1000, 3000attaches the servo on pin 18 to the servo object
  exeKeyServo.write(OFF_POS);
  delay(400);//移动到位时间
  exeKeyServo.detach();//释放舵机

  // You can remove the password parameter if you want the AP to be open.
  WiFi.mode(WIFI_AP); //设置工作在AP模式 混合模式 WIFI_AP_STA
  
  Serial.print("start AP .");

  //开始UDP服务
  WiFi.softAPConfig(localIp, gateway, subnet); //设置AP地址
  //delay(500);
  //Serial.print(".");
  while(!WiFi.softAP(localSsid, localPass,8,0,2)){
    Serial.print("X ");
  }; //启动AP 隐藏SSID
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP mac Address: ");
  Serial.println(WiFi.softAPmacAddress()); //打印mac地址
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  /* Serial.print("是否连接:");
  Serial.println(WiFi.isConnected());
  Serial.print("本地IP:");
  Serial.println(WiFi.localIP());
  Serial.print("本地IPv6:");
  Serial.println(WiFi.localIPv6());
  Serial.print("mac地址:");
  Serial.println(WiFi.macAddress());
  Serial.print("网络ID:");
  Serial.println(WiFi.networkID());
  Serial.print("是否休眠:");
  Serial.println(WiFi.getSleep());
  Serial.print("获取状态吗:");
  Serial.println(WiFi.getStatusBits());
  Serial.print("getTxPower:");
  Serial.println(WiFi.getTxPower());
  Serial.print("是否自动连接:");
  Serial.println(WiFi.getAutoConnect());
  Serial.print("是否自动重连:");
  Serial.println(WiFi.getAutoReconnect());
  Serial.print("获取模式:");
  Serial.println(WiFi.getMode());
  Serial.print("获取主机名:");
  Serial.println(WiFi.getHostname());
  Serial.print("获取网关IP:");
  Serial.println(WiFi.gatewayIP());
  Serial.print("dnsIP:");
  Serial.println(WiFi.dnsIP());
  Serial.print("状态:");
  Serial.println(WiFi.status());*/

  //开始UDP服务
  udp.begin(UDP_SERVER_PORT);
  Serial.println("Server started");
}

void loop() {
  
  if(udp.parsePacket()) {
    //Serial.print("AP connect count: ");
    digitalWrite(LED_BUILTIN, HIGH);//LOW 指示灯亮
    int pkSize=udp.read(buff,bfLength);//读取数据
    //处理命令,按下按钮
    exeKeyServo.attach(KEY_SERVO_PIN); // , 1000, 3000attaches the servo on pin 18 to the servo object
    exeKeyServo.write(ON_POS);
    delay(600);//移动到位时间
    exeKeyServo.write(OFF_POS);
    delay(400);//移动到位时间
    exeKeyServo.detach();//释放舵机

    udp.beginPacket(udp.remoteIP(),udp.remotePort());//设置
    udp.write('S');//返回处理结果
    udp.endPacket();
    Serial.write(buff,pkSize);
    Serial.println("revc:");
    memset(buff,0,bfLength);//清除缓冲器数据
    digitalWrite(LED_BUILTIN, LOW);//LOW 指示灯灭
  }

  //Serial.print("AP connect count: ");
  //Serial.println(WiFi.softAPgetStationNum()); //打印客户端连接数
}